﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Meta.Trade.Data;

namespace AAA.Trade.SignalCatcher
{
    public class TSCatcher : SignalCatcher
    {
        private ORTCLib.STCCOrders _STCCOrders;

        private ORTCLib.IFilledEvents_Event _eventFilledOrders;
        private ORTCLib.IActiveEvents_Event _eventActiveOrders;
        private ORTCLib.ICanceledEvents_Event _eventCanceledOrders;
        private ORTCLib.IOpenEvents_Event _eventOpenPositions;

        public TSCatcher()
        {
            try
            {
                _STCCOrders = new ORTCLib.STCCOrders();
                
                _eventFilledOrders = (ORTCLib.IFilledEvents_Event)_STCCOrders.FilledOrders;
                _eventActiveOrders = (ORTCLib.IActiveEvents_Event)_STCCOrders.ActiveOrders;
                _eventCanceledOrders = (ORTCLib.ICanceledEvents_Event)_STCCOrders.CanceledOrders;
                _eventOpenPositions = (ORTCLib.IOpenEvents_Event)_STCCOrders.OpenPositions;


                _eventFilledOrders.Add += FilledOrder_Add;
                _eventActiveOrders.Add += ActiveOrder_Add;
                _eventCanceledOrders.Add += CanceledOrder_Add;
                _eventOpenPositions.Add += OpenPosition_Add;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

        private void FilledOrder_Add(object sender)
        {
            if (OnFilledOrderEvent == null)
                return;

            ORTCLib.FilledOrder oFilledOrder = (ORTCLib.FilledOrder)sender;

            SignalInfo signalInfo = new SignalInfo();
            signalInfo.System = oFilledOrder.System;
            signalInfo.Symbol = oFilledOrder.Symbol;
            signalInfo.Order = oFilledOrder.Order;
            signalInfo.OrderType = oFilledOrder.OrderType;
            signalInfo.Interval = oFilledOrder.Interval;
            signalInfo.SlippagePrice = Convert.ToString(oFilledOrder.SlippagePrice);
            signalInfo.FillPrice = Convert.ToString(oFilledOrder.FillPrice);
            signalInfo.TimeFilled = oFilledOrder.TimeFilled;            
            signalInfo.Workspace = oFilledOrder.WorkSpace;
            signalInfo.Description = oFilledOrder.Description;
            signalInfo.Signal = oFilledOrder.Signal;
            signalInfo.Strategy = signalInfo.System.Substring(0, signalInfo.System.IndexOf("("));

            OnFilledOrderEvent(signalInfo);
            
        }

        private void ActiveOrder_Add(object sender)
        {
            if (OnActiveOrderEvent == null)
                return;

            ORTCLib.ActiveOrder oActiveOrder = (ORTCLib.ActiveOrder)sender;

            SignalInfo signalInfo = new SignalInfo();
            signalInfo.System = oActiveOrder.System;
            signalInfo.Symbol = oActiveOrder.Symbol;
            signalInfo.Order = oActiveOrder.Order;
            signalInfo.OrderType = oActiveOrder.OrderType;
            signalInfo.Interval = oActiveOrder.Interval;
            signalInfo.TimePlaced = oActiveOrder.TimePlaced;
            signalInfo.Workspace = oActiveOrder.WorkSpace;
            signalInfo.Description = oActiveOrder.Description;
            signalInfo.LastPrice = Convert.ToString(oActiveOrder.LastPrice);
            signalInfo.Signal = oActiveOrder.Signal;
            signalInfo.Strategy = signalInfo.System.Substring(0, signalInfo.System.IndexOf("("));
            OnActiveOrderEvent(signalInfo);

        }

        private void CanceledOrder_Add(object sender)
        {
            if (OnCanceledOrderEvent == null)
                return;

            ORTCLib.CanceledOrder oCanceledOrder = (ORTCLib.CanceledOrder)sender;

            SignalInfo signalInfo = new SignalInfo();
            signalInfo.System = oCanceledOrder.System;
            signalInfo.Symbol = oCanceledOrder.Symbol;
            signalInfo.Order = oCanceledOrder.Order;
            signalInfo.OrderType = oCanceledOrder.OrderType;
            signalInfo.Interval = oCanceledOrder.Interval;
            signalInfo.TimeCanceled = oCanceledOrder.TimeCanceled;
            signalInfo.Workspace = oCanceledOrder.WorkSpace;
            signalInfo.Description = oCanceledOrder.Description;
            signalInfo.Signal = oCanceledOrder.Signal;
            signalInfo.Strategy = signalInfo.System.Substring(0, signalInfo.System.IndexOf("("));
            OnCanceledOrderEvent(signalInfo);            
        }

        private void OpenPosition_Add(object sender)
        {
            if (OnOpenPositionEvent == null)
                return;

            ORTCLib.OpenPosition oOpenPosition = (ORTCLib.OpenPosition)sender;            
            SignalInfo signalInfo = new SignalInfo();
            signalInfo.System = oOpenPosition.System;
            signalInfo.Symbol = oOpenPosition.Symbol;
            signalInfo.Interval = oOpenPosition.Interval;
            signalInfo.Workspace = oOpenPosition.WorkSpace;
            signalInfo.Description = oOpenPosition.Description;
            signalInfo.Signal = oOpenPosition.Signal;
            signalInfo.Strategy = signalInfo.System.Substring(0, signalInfo.System.IndexOf("("));
            signalInfo.FillPrice = Convert.ToString(oOpenPosition.EntryPrice);
            signalInfo.TimeFilled = oOpenPosition.EntryTime;
            signalInfo.EntryPrice = Convert.ToString(oOpenPosition.EntryPrice);
            signalInfo.EntryTime = oOpenPosition.EntryTime;            
            
            OnOpenPositionEvent(signalInfo);
        }

    }

}
